//
//  WSSureOrderHdV.m
//  FD
//
//  Created by xiaofang wang on 2018/11/5.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "WSSureOrderHdV.h"
@interface WSSureOrderHdV()
/**送餐方式*/
@property (nonatomic, strong) UILabel *titleLb;
/**2.外送按钮*/
@property (nonatomic, strong) UIButton *waisongBtn;
/**3.自提*/
@property (nonatomic, strong) UIButton *ziTiBtn;
@end
@implementation WSSureOrderHdV
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}
- (void)setupView
{
    CGFloat sh = self.bounds.size.height;
    
    CGFloat x = 15;
    CGFloat h = 22;
    CGFloat y = (sh - h)*0.5;
    self.titleLb = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 130, h)];
    [self addSubview:self.titleLb];
   
    //单独处理国际化
    if ([NSString rb_ChineseLanguage]) {
        self.titleLb.text = @"送餐方式";
    }else {
        self.titleLb.text = @"delivery method";
    }
    
    //国际化词条有毒
//    self.titleLb.text = NSLocalizedString(deliveryMethod, @"送餐方式");
    
    [self addSubview:self.waisongBtn];
    [self addSubview:self.ziTiBtn];
    //坑，国际化适配问题
    
    //默认选中外送
    self.waisongBtn.selected = YES; //默认选中    
}


- (void)layoutSubviews {
    [super layoutSubviews];
    //计算外送 自提文字长度
    CGFloat img_w = 20; //圆形按钮文字固定长度为 20
    CGFloat h = 20;
    
    //国际l
    NSString *wsStr = NSLocalizedString(Home_Delivery, @"外送");
    NSString *ztStr = NSLocalizedString(Home_SelfLifting, @"自提");

    CGSize wsSize = [wsStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
    CGFloat ws_w = wsSize.width * 1.2 + img_w + 5; //放大到1.2倍
    
    CGSize ztSize = [ztStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
    CGFloat zt_w = ztSize.width *1.2 + img_w + 5;
    
    [self.ziTiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-10); //距离右边10
        make.size.mas_equalTo(CGSizeMake(zt_w, h));
        make.centerY.equalTo(self.mas_centerY);
    }];
    
    
    [self.waisongBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(CGSizeMake(ws_w, h));
        make.right.equalTo(self.ziTiBtn.mas_left); //左边挨着右边
        make.size.mas_equalTo(CGSizeMake(ws_w, h));
    }];
}

- (UIButton *)waisongBtn {
    if (!_waisongBtn) {
        _waisongBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_waisongBtn addTarget:self action:@selector(clickWaiSongAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
//        @"外送"
        NSString *wsStr = NSLocalizedString(Home_Delivery, @"外送");
        [_waisongBtn setTitle:wsStr forState:(UIControlStateNormal)];
        [_waisongBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_waisongBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_waisongBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
    }
    return _waisongBtn;
}
- (UIButton *)ziTiBtn {
    if (!_ziTiBtn) {
        _ziTiBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_ziTiBtn addTarget:self action:@selector(clickZiTiAction:) forControlEvents:(UIControlEventTouchUpInside)];
//        @"自提"
        //国际化
        NSString *ztStr = NSLocalizedString(Home_SelfLifting, @"自提");

        [_ziTiBtn setTitle:ztStr forState:(UIControlStateNormal)];
        [_ziTiBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_ziTiBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_ziTiBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
    }
    return _ziTiBtn;
}

#pragma mark - 点击事件 clickWaiSongAction
- (void)clickWaiSongAction:(UIButton *)btn
{
    FDLog(@"外送");
    if (self.clickTypeBlock) {
        self.clickTypeBlock(WSSureOrderHdvTypeWS) ;//点击的是外送
    }
    
    //逻辑判断， 只有一个被选中，始终都是选中状态
    btn.selected = YES;
    self.ziTiBtn.selected = NO;
}

- (void)clickZiTiAction:(UIButton *)btn
{
    btn.selected = YES;
    self.waisongBtn.selected = NO;
    
//    //切换按钮的状态
//    btn.selected = !btn.selected;
//    if (btn.selected) {
//        self.waisongBtn.selected = NO;
//    }
    
    FDLog(@"自提");
    if (self.clickTypeBlock) {
        self.clickTypeBlock(WSSureOrderHdvTypeZT) ;//点击的是自提
    }
}

@end

